#include <QAction>
#include <QPainter>
#include <QBoxLayout>
#include <QFrame>

#include "gui/toolbutton.h"
#include "gui/harustyle.h"

#include "maintoolbar.h"

#define WidgetHeight    (22)
#define TopMargin       (3)

MainToolBar::MainToolBar(QWidget *parent) : QWidget(parent),
    m_layout(new QBoxLayout(QBoxLayout::LeftToRight, this)),
    m_pointer(0)
{
    //Set properties.
    setContentsMargins(4, TopMargin, 4, TopMargin + 1);
    setAutoFillBackground(true);
    setLayout(m_layout);
    setFixedHeight(WidgetHeight + (TopMargin << 1) + 1);
    setStyle(HaruStyle::instance());
    //Update the palette.
    QPalette pal = palette();
    pal.setColor(QPalette::WindowText, QColor(205, 205, 205));
    setPalette(pal);
    //Configure layouts.
    m_layout->setContentsMargins(0, 0, 0, 0);
    m_layout->addStretch();
}

void MainToolBar::addAction(QAction *action)
{
    //Create a button for the action.
    ToolButton *button = new ToolButton(this);
    button->setIcon(action->icon());
    button->setToolTip(action->text());
    //Add button to toolbar.
    addButton(button);
}

void MainToolBar::addButton(QToolButton *button)
{
    //Update the button size.
    button->setFixedSize(WidgetHeight, WidgetHeight);
    //Add button to layout.
    m_layout->insertWidget(m_pointer++, button);
}

void MainToolBar::addSplitter()
{
    //Create a splitter widget.
    QFrame *splitter = new QFrame(this);
    splitter->setFrameStyle(QFrame::VLine);
    //Add widget.
    addWidget(splitter);
}

void MainToolBar::addWidget(QWidget *widget)
{
    //Change the widget height.
    widget->setFixedHeight(WidgetHeight);
    //Add the widget to layout.
    m_layout->insertWidget(m_pointer++, widget);
}

void MainToolBar::paintEvent(QPaintEvent *)
{
    //Draw the border line.
    QPainter painter(this);
    int y = height() - 1;
    painter.drawLine(0, y, width(), y);
}
